//版本更新
/*获取版本*/
function checkVersion() {
    var systemType = api.systemType;
    var appVersion = api.appVersion;
    var version_arr = appVersion.split('.');
    api.ajax({
        url: baseUrl + '/version_update',
        method: 'post'
    }, function (ret, err) {
        wx.hideLoading();
        if (ret) {
            if (ret.code == 1) {
                var gengxin_desc = ret.data.update_desc;
                if (systemType == 'android') {
                    var android = ret.data.android_version;
                    var android_arr = android.split('.');
                    var android_url = ret.data.android_url;
                    if (android && android_url) {
                        if (parseInt(android_arr[0]) > parseInt(version_arr[0])) {
                            versionUpdating(android_url, gengxin_desc);
                        } else {
                            if (parseInt(android_arr[1]) > parseInt(version_arr[1])) {
                                versionUpdating(android_url, gengxin_desc);
                            } else {
                                if (parseInt(android_arr[2]) > parseInt(version_arr[2])) {
                                    versionUpdating(android_url, gengxin_desc);
                                }
                            }
                        }
                    }
                } else {
                    var ios = ret.data.ios_version;
                    var ios_arr = ios.split('.');
                    var ios_url = ret.data.ios_url;
                    if (ios && ios_url) {
                        if (parseInt(ios_arr[0]) > parseInt(version_arr[0])) {
                            versionUpdating(ios_url, gengxin_desc);
                        } else {
                            if (parseInt(ios_arr[1]) > parseInt(version_arr[1])) {
                                versionUpdating(ios_url, gengxin_desc);
                            } else {
                                if (parseInt(ios_arr[2]) > parseInt(version_arr[2])) {
                                    versionUpdating(ios_url, gengxin_desc);
                                }
                            }
                        }
                    }
                }
            }
        }
    });
}

/*版本更新*/
function versionUpdating(url, content) {
    var systemType = api.systemType;
    var dialogBox = api.require('dialogBox');
    dialogBox.tips({
        rect: {
            w: 300,
        },
        texts: {
            title: '检测到版本更新',
            content: content
        },
        buttons: [{
            normal: 'widget://res/no.png',
            highlight: 'widget://res/no.png',
        }, {
            normal: 'widget://res/yes.png',
            highlight: 'widget://res/yes.png',

        }],
        tapClose: false,
        styles: {
            bg: '#FFF',
            topImage: {
                path: 'widget://res/gengxin.png',
                w: 300,
                h: 120
            },
            title: {
                color: '#000',
                size: 16,
                marginT: 10
            },
            content: {
                color: '#333',
                size: 12,
                marginT: 10
            },
            border: {
                color: '#eee',
                width: 2,
                marginT: 20
            },
            buttons: [{
                iconSize: 50,
                textColor: '#000',
                textSize: 0,
                marginT: 10,
                space: 100
            }, {
                iconSize: 50,
                textColor: '#000',
                textSize: 0,
                marginT: 10,
                space: 100
            }]
        }
    }, function (ret) {
        if (ret.index == 0) {
            api.closeWidget({
                id: 'A6018394391026',
                silent: true
            });
            dialogBox.close({
                dialogName: 'tips'
            });
        } else {
            dialogBox.close({
                dialogName: 'tips'
            });
            if (systemType == 'android') {
                versionUpdating_android(url);
            } else {
                versionUpdating_ios(url);
            }
        }
    });
}

/*android*/
function versionUpdating_android(url) {
    var UIActionProgress = api.require('UIActionProgress');
    UIActionProgress.open({
        maskBg: 'rgba(0,0,0,0.5)',
        styles: {
            h: 108,
            bg: '#fff',
            title: {
                size: 16,
                color: '#000',
                marginT: 10
            },
            msg: {
                size: 12,
                color: '#000',
                marginT: 5
            },
            lable: {
                size: 14,
                color: '#696969',
                marginB: 5
            },
            progressBar: {
                size: 3,
                normal: '#999',
                active: '#FB5C59',
                marginB: 35,
                margin: 5
            }
        },
        data: {
            title: '正在更新',
            value: 0
        }
    }, function (ret) {
        if (ret.eventType == 'show') {
            api.download({
                url: url,
                savePath: 'fs://dashendailian.apk',
                report: true,
                cache: false,
                allowResume: true
            }, function (ret, err) {
                if (ret.state == 1) {
                    UIActionProgress.close();
                    api.installApp({
                        appUri: 'fs://dashendailian.apk'
                    });
                } else {
                    UIActionProgress.setData({
                        data: {
                            title: '正在更新...',
                            value: ret.percent
                        }
                    });
                }
            });
        }
    });
}

/*ios*/
function versionUpdating_ios(url) {
    var openAppStore = api.require('openAppStore');
    openAppStore.appStore({
        appID: url
    });
}
/*
 * 版本号比较方法
 * 传入两个字符串，当前版本号：curV；比较版本号：reqV
 * 调用方法举例：compare("1.1","1.2")，将返回false
 */
function compare(curV, reqV) {
    if (curV && reqV) {
        //将两个版本号拆成数字
        var arr1 = curV.split('.'),
        arr2 = reqV.split('.');
        var minLength = Math.min(arr1.length, arr2.length),
        position = 0,
        diff = 0;
        //依次比较版本号每一位大小，当对比得出结果后跳出循环（后文有简单介绍）
        while (position < minLength && ((diff = parseInt(arr1[position]) - parseInt(arr2[position])) == 0)) {
        position++;
        }
        diff = (diff != 0) ? diff : (arr1.length - arr2.length);
        //若curV大于reqV，则返回true
        return diff < 0;
    } else {
        //输入为空
        console.log("版本号不能为空");
        return false;
    }
}